package com.bluse.controller;

import com.bluse.client.HttpClient;
import com.bluse.service.RedisService;
import com.bluse.service.UserService;
import com.bluse.service.dto.UserDto;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Describe: TODO
 * @Author: bluse
 * @Date: 2018/12/26 11:18
 **/

//@RestController
@RequestMapping("/api")
@Api(value = "用户", description = "用户相关api")
public class UserController {

    private UserService userService;

    private HttpClient httpClient;

    private RedisService redisService;

    public UserController(UserService userService, HttpClient httpClient
            , RedisService redisService) {
        this.userService = userService;
        this.httpClient = httpClient;
        this.redisService = redisService;
    }


    /**
     * get use list
     *
     * @return
     */
    @GetMapping("/user")
    @ApiOperation(nickname = "获取用户列表", value = "获取用户列表")
    public List<UserDto> list() {
        return userService.findAll();
    }

    /**
     * get user By userId
     *
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    @ApiOperation(nickname = "Get user by userID", value = "获取用户信息")
    public Object getUser(@PathVariable("id") String id) {

        Object value = redisService.getValue(id);
        if (value == null) {
            return userService.findUserById(id);
        }
        return value;
    }


    /**
     * add a new  user
     *
     * @param userDto
     * @return
     */
    @PostMapping("/user")
    @ApiOperation(value = "新增用户")
    public ResponseEntity<UserDto> saveUser(@RequestBody UserDto userDto) {
        redisService.setValue(userDto.getId(), userDto);
        UserDto result = userService.save(userDto);
        return ResponseEntity.ok(result);
    }


}
